package swimv2gm.client.user;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2gm.bl.session.FriendshipManager;

/**
 * Rimuove un amico o una richiesta di amicizia inviata.
 * 
 * @author Matteo Gallesio
 */
public class RemoveFriend extends HttpServlet {

	private static final long serialVersionUID = -2250880080464674906L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		FriendshipManager man;
		try {
			man = (FriendshipManager)InitialContext.doLookup("FriendshipManager");
		} catch (NamingException e) {
			response.sendError(500);
			return;
		}
		Long myId = (Long)request.getSession().getAttribute("user");

		if (myId == null) {
			response.sendError(403);
			return;
		}

		boolean success;
		try {
			long friend = Long.parseLong(request.getParameter("friend"));
			success = man.removeFriendship(myId,friend);
		} catch (IllegalArgumentException e) {
			success = false;
		}

		response.sendRedirect("/SWIMv2gm/user/FriendList?removed=" + success);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		doGet(request,response);
	}

}
